package org.example.tree.controller;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.tree.domain.Four;
import org.example.tree.domain.One;
import org.example.tree.domain.Three;
import org.example.tree.domain.Two;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TreeController {
    @ResponseBody
    @GetMapping ("/test1")
    public Map<String, Object> fun1(String name){
        Map<String,Object> result = new HashMap<>();
        result.put("name", name);
        result.put("age",29);
        return result;
    }

    @ResponseBody
    @PostMapping("/test2")
    public Map<String, Object> fun2(){
        Map<String,Object> result = new HashMap<>();
        result.put("name","piggy");
        result.put("age",29);
        return result;
    }

    @ResponseBody
    @GetMapping("/test3")
    public String fun3() throws IOException {
        List<One> oneList = new ArrayList<>();
        One one1 = new One();
        one1.setOne("one1");
        oneList.add(one1);
        oneList.add(one1);
        List<Two> twoList1 = new ArrayList<>();
        Two two1 = new Two();
        two1.setTwo("two1");
        twoList1.add(two1);
        List<Three> threeList1 = new ArrayList<>();
        Three three1_1 = new Three();
        three1_1.setThree("three1");
        threeList1.add(three1_1);
        Three three1_2 = new Three();
        three1_2.setThree("three2");
        threeList1.add(three1_2);

        List<Four> fourList1 = new ArrayList<>();
        Four four1 = new Four();
        four1.setFour("four1");
        Four four2 = new Four();
        four2.setFour("four2");
        fourList1.add(four1);
        fourList1.add(four2);

        List<Four> fourList2 = new ArrayList<>();
        Four four3 = new Four();
        four3.setFour("four3");
        fourList2.add(four3);

        one1.setTwoList(twoList1);
        two1.setThreeList(threeList1);
        three1_1.setFourList(fourList1);
        three1_2.setFourList(fourList2);


        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        int count = 0;
        for(int i=0; i<oneList.size(); i++){
            int aaa = count;
            One one = oneList.get(i);
            List<Two> twoList = one.getTwoList();
            for(int j=0; j<twoList.size(); j++){
                int bbb = count;
                Two two = twoList.get(j);
                List<Three> threeList = two.getThreeList();
                for(int k=0; k<threeList.size(); k++){
                    int ccc = count;
                    Three three = threeList.get(k);
                    List<Four> fourList = three.getFourList();
                    for(int z=0; z<fourList.size(); z++){
                        Row row = sheet.createRow(count);
                        Cell cell = row.createCell(3);
                        Four four = fourList.get(z);
                        cell.setCellValue(four.getFour());
//                        cell = row.createCell(4);
//                        cell.setCellValue(i + j + k + z);
                        count++;
                    }
                    if(ccc != count-1){
                        CellRangeAddress cellRangeAddress = new CellRangeAddress(ccc, count-1, 2, 2);
                        sheet.addMergedRegion(cellRangeAddress);
                    }
                    Row row = sheet.getRow(ccc);
                    Cell cell = row.createCell(2);
                    Three three2 = threeList.get(k);
                    cell.setCellValue(three2.getThree());
                }
                if(bbb != count-1){
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(bbb, count-1, 1, 1);
                    sheet.addMergedRegion(cellRangeAddress);
                }
                Row row = sheet.getRow(bbb);
                Cell cell = row.createCell(1);
                Two two2 = twoList.get(j);
                cell.setCellValue(two2.getTwo());
            }
            if(aaa != count-1){
                CellRangeAddress cellRangeAddress = new CellRangeAddress(aaa, count-1, 0, 0);
                sheet.addMergedRegion(cellRangeAddress);
            }
            Row row = sheet.getRow(aaa);
            Cell cell = row.createCell(0);
            One one2 = oneList.get(i);
            cell.setCellValue(one2.getOne());
        }

        FileOutputStream out = new FileOutputStream("merged_cells.xlsx");
        workbook.write(out);
        out.close();
        workbook.close();
        return "";
    }
}
